package com.aisb.gis.util;

import com.aisb.core.util.file.TxtFileUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapUtil {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    private final float WIDTH_CIRCUMFERENCE = (float) (40076000 * Math.cos(2 * Math.PI / 360 * 35));
    private final float HEIGHT_CIRCUMFERENCE = 39900000;

    // 中国
//    private final int MAXLNG = 140;
//    private final int MINLNG = 70;
//    private final int MAXLAT = 60;
//    private final int MINLAT = 5;

    // 陕西
    private final int MAXLNG = 112;
    private final int MINLNG = 105;
    private final int MAXLAT = 40;
    private final int MINLAT = 31;

    private int width = 500;
    private int height = 500;
    private float minWidth;
    private float minHeight;

    private void setWidhtHeight(int width, int height) {
        this.width = width;
        this.height = height;
    }

    private void setMinWidthHeight() {
        minWidth = 360 / WIDTH_CIRCUMFERENCE * width;
        minHeight = 360 / HEIGHT_CIRCUMFERENCE * height;
    }

    public void getChinaCode() {
        setMinWidthHeight();

        int widthNum = (int) ((MAXLNG - MINLNG) / minWidth + 1);
        int heightNum = (int) ((MAXLAT - MINLAT) / minHeight + 1);
        System.out.println(widthNum);
        System.out.println(heightNum);
        widthNum = 20;
        heightNum = 20;
        System.out.println(widthNum);
        System.out.println(heightNum);
        List<Map<String, Object>> rList = new ArrayList<>();
        List<Object> roList = new ArrayList<>();
        Map<String, Object> rMap;
        int lngStart = 550;
        int latStart = 550;
        for (int i = latStart; i < latStart + heightNum; i++) {
            for (int j = lngStart; j < lngStart + widthNum; j++) {
                rMap = new HashMap<>();
                float lngc = MINLNG + j * minWidth;
                float latc = MINLAT + i * minHeight;
                rMap.put("lng", lngc);
                rMap.put("lat", latc);
                rList.add(rMap);
                roList.add(lngc + "," + latc + ";");
                System.out.println(lngc + "," + latc + ";");
            }
        }

//        logger.debug("{}", roList.size());
        System.out.println(roList.size());
    }

    public String getLacciCode(String lnglat) {
        lnglat = lnglat.replace(";", "");
        String[] lnglatarr = lnglat.split(",");
        if(lnglatarr.length < 3){
            return "";
        }
        String lng = lnglatarr[1];
        String lat = lnglatarr[2];
        if (lng.length() < 5 || lat.length() < 4) {
            return "";
        }

        float lngf = Float.parseFloat(lng);
        float latf = Float.parseFloat(lat);

        setMinWidthHeight();

        int widthNum = (int) ((lngf - MINLNG) / minWidth);
        int heightNum = (int) ((latf - MINLAT) / minHeight);

        float lngc = MINLNG + widthNum * minWidth + minWidth / 2;
        float latc = MINLAT + heightNum * minHeight + minHeight / 2;
        float lngmax = MINLNG + widthNum * minWidth + minWidth;
        float lngmin = MINLNG + widthNum * minWidth;
        float latmax = MINLAT + heightNum * minHeight + minHeight;
        float latmin = MINLAT + heightNum * minHeight;

        String result = lng + "," + lat + "," + 1 + "," + lngc + "," + latc + "," + 1 + "," + lngmax + "," + lngmin + "," + latmax + "," + latmin + "," + width + "," + height + ";";
//        System.out.println(result);

        result = lnglat + "," + lngc + "," + latc + "," + new Md5Hash((lngc+"&"+latc).getBytes());

        return result;

    }

    public void getLacciCode(List<String> lnglatList) {
        List<String> ghCenterSet = new ArrayList<>();

        for (int i = 0; i < lnglatList.size(); i++) {
            String lnglat = lnglatList.get(i).trim();
            if (StringUtils.isBlank(lnglat)) {
                continue;
            }


            ghCenterSet.add(getLacciCode(lnglat));
        }

        String filePath = "C:\\Users\\Administrator\\Desktop\\dim_lac_cell_info\\td_dim_lac_cell_info_结果.txt";
        if (ghCenterSet.size() > 20) {
            TxtFileUtil.write(filePath, ghCenterSet);
        } else {
            System.out.println(com.sun.deploy.util.StringUtils.join(ghCenterSet, "\n\r"));
        }
    }

    public static List<String> lacciData() {
        String filePath = "C:\\Users\\Administrator\\Desktop\\dim_lac_cell_info\\td_dim_lac_cell_info.csv";
        List<String> list = TxtFileUtil.readList(filePath);

        return list;
    }

    public static List<String> lacciData2() {
        List<String> list = new ArrayList<>();
        list.add("108.962222,34.259722");
        list.add("108.958277, 34.259536;");

        return list;
    }

    public static void main(String[] args) {
        MapUtil mu = new MapUtil();
//        mu.getChinaCode();'
//        mu.getLacciCode("108.956666", "34.258611");
        mu.getLacciCode(lacciData());
    }

}
